package cn.tedu.oa_system.service.impl;

import cn.tedu.oa_system.base.json_result.ResultDataDto;
import cn.tedu.oa_system.mapper.UserLoginRecordMapper;
import cn.tedu.oa_system.pojo.UserLoginRecord;
import cn.tedu.oa_system.pojo.in.FindLikeKeyIn;
import cn.tedu.oa_system.pojo.in.PageIn;
import cn.tedu.oa_system.service.UserLonginRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author MW
 * @Description 在线业务服务接口实现类
 * @CreateDate 2023/2/26
 */
@Service
public class UserLoginRecordServiceImpl implements UserLonginRecordService {

    @Autowired
    UserLoginRecordMapper mapper;

    @Override
    public ResultDataDto<UserLoginRecord> list(PageIn pageIn) {
        LambdaQueryWrapper<UserLoginRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(UserLoginRecord::getRecordId,-1);
        ResultDataDto<UserLoginRecord> resultDataDto = new ResultDataDto<>();
        Page<UserLoginRecord> page = new Page(pageIn.getCurrent(), pageIn.getSize());
        Page<UserLoginRecord> userLoginRecordPage = mapper.selectPage(page, queryWrapper);
        List<UserLoginRecord> records = userLoginRecordPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userLoginRecordPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }

    @Override
    public ResultDataDto<UserLoginRecord> findBaseKey(FindLikeKeyIn baseKeyIn) {
        LambdaQueryWrapper<UserLoginRecord> queryWrapper = new LambdaQueryWrapper<>();
        String baseKey = baseKeyIn.getLikeKey();
        queryWrapper.like(UserLoginRecord::getIpAddr,baseKey).or()
                    .like(UserLoginRecord::getLoginTime,baseKey).or()
                    .like(UserLoginRecord::getBrowser,baseKey);
        ResultDataDto<UserLoginRecord> resultDataDto = new ResultDataDto<>();
        Page<UserLoginRecord> page = new Page(baseKeyIn.getCurrent(), baseKeyIn.getSize());
        Page<UserLoginRecord> userLoginRecordPage = mapper.selectPage(page, queryWrapper);
        List<UserLoginRecord> records = userLoginRecordPage.getRecords();
        resultDataDto.setDataList(records);
        resultDataDto.setTotal(userLoginRecordPage.getTotal());
        resultDataDto.setIsSuccess(true);
        return resultDataDto;
    }
}
